package com.example.newsapp.view.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.newsapp.R;
import com.example.newsapp.model.entity.VideoItem;
import com.example.newsapp.view.activity.VideoPlayActivity;

import java.util.ArrayList;
import java.util.List;

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHolder> {
    private List<VideoItem> videoList = new ArrayList<>();

    @NonNull
    @Override
    public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video, parent, false);
        return new VideoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) {
        VideoItem videoItem = videoList.get(position);

        holder.titleTextView.setText(videoItem.getTitle());
        holder.sourceTextView.setText(videoItem.getSource());
        holder.timeTextView.setText(videoItem.getPtime());
        holder.durationTextView.setText(formatDuration(videoItem.getLength()));
        holder.replyCountTextView.setText(videoItem.getReplyCount() + "评");
        holder.likeCountTextView.setText(videoItem.getVoteCount() + "赞");

        // 加载封面图
        Glide.with(holder.itemView.getContext())
                .load(videoItem.getCover())
                .placeholder(R.drawable.video_placeholder)
                .error(R.drawable.video_error)
                .into(holder.coverImageView);

        // 点击事件
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(v.getContext(), VideoPlayActivity.class);
            intent.putExtra("videoUrl", videoItem.getMp4_url());
            intent.putExtra("title", videoItem.getTitle());
            v.getContext().startActivity(intent);
        });
    }
    // 添加加载更多方法
    public void addVideoList(List<VideoItem> newVideos) {
        int startPos = this.videoList.size();
        this.videoList.addAll(newVideos);
        notifyItemRangeInserted(startPos, newVideos.size());
    }
    @Override
    public int getItemCount() {
        return videoList.size();
    }

    public void setVideoList(List<VideoItem> videoList) {
        this.videoList = videoList;
        notifyDataSetChanged();
    }

    private String formatDuration(int seconds) {
        int minutes = seconds / 60;
        seconds = seconds % 60;
        return String.format("%02d:%02d", minutes, seconds);
    }

    static class VideoViewHolder extends RecyclerView.ViewHolder {
        ImageView coverImageView;
        TextView titleTextView;
        TextView sourceTextView;
        TextView timeTextView;
        TextView durationTextView;
        TextView replyCountTextView;
        TextView likeCountTextView;

        public VideoViewHolder(@NonNull View itemView) {
            super(itemView);
            coverImageView = itemView.findViewById(R.id.imageViewCover);
            titleTextView = itemView.findViewById(R.id.textViewTitle);
            sourceTextView = itemView.findViewById(R.id.textViewSource);
            timeTextView = itemView.findViewById(R.id.textViewTime);
            durationTextView = itemView.findViewById(R.id.textViewDuration);
            replyCountTextView = itemView.findViewById(R.id.textViewReplyCount);
            likeCountTextView = itemView.findViewById(R.id.textViewLikeCount);
        }
    }
}